﻿using Newtonsoft.Json;
using SaleManagement.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SaleManagement.Controllers
{
    public class IndustriesController
    {
        private List<Industries_Model> ListIndestries;
        public string mess = "";
        public IndustriesController()
        {
            ListIndestries = new List<Industries_Model>();
        }
        public List<Industries_Model> getdata(int id)
        {
            string urlAddress = "http://192.168.0.106/sale-server/public/" + id + "/industries";
            using (WebClient client = new WebClient())
            {
                String value = client.DownloadString(urlAddress);//lay json tu web ve
                ListIndestries = JsonConvert.DeserializeObject<List<Industries_Model>>(value);
            }
            return ListIndestries;
        }//End

        public bool insertIndustry(int id, string name, string description)
        {
            bool kt = false;
            string urlAddress = "http://192.168.0.106/sale-server/public/" + id + "/industry/insert";
            using (WebClient client = new WebClient())
            {
                NameValueCollection postdata = new NameValueCollection(){
                    {"name",name},
                    {"description",description},
                };
                String pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, "POST", postdata));
                bool status = pagesource.Contains("status");
                if (status == true)
                {
                    Status_Model mdStatus = JsonConvert.DeserializeObject<Status_Model>(pagesource);
                    mess = mdStatus.Status;
                    kt = true;

                }
                else
                {
                    Error_Model mdError = JsonConvert.DeserializeObject<Error_Model>(pagesource);
                    mess = mdError.Error_msg;
                    kt = false;
                }
            }
            return kt;
        }//End 

    }
}
